/**
 * @param {number} n
 * @return {number[]}
 */
 var lexicalOrder = function(n) {
  const ret = new Array(n).fill(0);
  let now = 0;
  function findChild(num) {
      let i = 0;
      while(num + i <= n && i < 10) {
          ret[now] = num + i;
          now++;
          if(num * 10 <= n) {
              findChild((num + i) * 10);
          }
          i++;
      }
  }
  for(let g = 1; g <= Math.min(n, 9); g++) {
      ret[now] = g;
      now++;
      if (g * 10 <=n ) findChild(g * 10);
  }
  return ret;
};